#include "owned_string.h"

namespace krryn{
namespace core{
	owned_string::owned_string() 
		: m_Data(0), m_Length(0) {}
	owned_string::owned_string(char *a_Data, size_t a_Length)
		: m_Data(a_Data), m_Length(a_Length) {}

	owned_string owned_string::trim(char l_ToTrim ){
		owned_string l_Result(*this);

		while(*l_Result.m_Data == l_ToTrim)
			l_Result.m_Data++;

		while(l_Result.m_Data[l_Result.m_Length-1] == l_ToTrim)
			l_Result.m_Length--;

		return l_Result;
	}

	bool owned_string::strcmp(char *a_First, const char *a_SecondNullTerminated, size_t a_FirstLength) const{
		for(size_t i = 0; i < a_FirstLength; i++){
			if(a_First[i] != a_SecondNullTerminated[i]) return true;
		}

		// This condition can happen if a_First is a substring of a_SecondNullTerminated
		return a_SecondNullTerminated[a_FirstLength] != 0;
	}

	bool owned_string::strcmp(char *a_First, const char *a_Second, size_t a_FirstLength, size_t a_SecondLength) const{
		if(a_FirstLength != a_SecondLength) return true;

		for(size_t i = 0; i < a_FirstLength; i++){
			if(a_First[i] != a_Second[i]) return true;
		}

		return false;
	}
	
	std::ostream &operator<<(std::ostream &a_Out, const owned_string &a_Str){
		for(size_t i = 0; i < a_Str.m_Length; i++){
			a_Out << a_Str.m_Data[i];
		}
		return a_Out;
	}

	bool operator==(const std::string &a_Other, const owned_string &a_Str){
		return !a_Str.strcmp(a_Str.m_Data, a_Other.c_str(), a_Str.m_Length);
	}
}
}